<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="75px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="申报年度:" prop="sbNd">
            <el-select v-model="queryParams.sbNd" placeholder="请选择申报年度" clearable>
              <el-option
                v-for="dict in dict.type.year"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="申报单位:" prop="sbDw">
            <el-select v-model="queryParams.sbDw" placeholder="请选择申报单位" clearable>
              <el-option
                v-for="dict in dict.type.xmgl_lxwj_pzdw"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="流程状态:" prop="processStatus">
            <el-select v-model="queryParams.processStatus" placeholder="请选择流程状态" clearable size="mini">
              <el-option
                v-for="dict in dict.type.process_business_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['khpx:cxdw:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold',fontSize: '12px'}"
              height="300" class="fixedTableTwo" v-loading="loading" :data="cxdwList"
              @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
      <el-table-column label="申报年度" align="center" prop="sbNd" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.year" :value="scope.row.sbNd"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="申报单位" align="center" prop="sbDw" width="180">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">
              <dict-tag :options="dict.type.xmgl_lxwj_pzdw" :value="scope.row.sbDw"/>
            </el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="年度产值" align="center" prop="ndCz"/>
      <el-table-column show-overflow-tooltip label="研发投入" align="center" prop="yfTr"/>
      <el-table-column show-overflow-tooltip label="专利总数(项)" align="center" prop="zlZs"/>
      <el-table-column show-overflow-tooltip label="省部工法(项)" align="center" prop="sbGf"/>
      <el-table-column show-overflow-tooltip label="企业工法(项)" align="center" prop="qyGf"/>
      <el-table-column show-overflow-tooltip label="软件著作权(项)" align="center" prop="rjZzq"/>
      <el-table-column show-overflow-tooltip label="省部论文(篇)" align="center" prop="sbLw"/>
      <el-table-column show-overflow-tooltip label="省部科技奖(项)" align="center" prop="sbKjj"/>
      <el-table-column show-overflow-tooltip label="立项项目(数)" align="center" prop="lxXm"/>
      <el-table-column show-overflow-tooltip label="公司科技奖(项)" align="center" prop="gsKjj"/>
      <el-table-column label="状态" align="center" prop="processStatus" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.process_business_status" :value="scope.row.processStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleJinDu(scope.row)"
            v-if="scope.row.processStatus !== 1"
          >查询进度
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 预览组件 -->
    <KkFileView ref="preview"/>

    <!-- 查询进度 -->
    <Jindu ref="picture"/>

    <!-- 创新单位详情 -->
    <CxDwXq ref="cxDwXq"/>
  </div>
</template>

<script>
import {listCxdw,} from "@/api/khpx/cxdw";
import {getToken} from "@/utils/auth";
import KkFileView from "@/views/components/KKFileView/kkFileView.vue";
import Jindu from "@/views/components/liuChengState/jindu.vue";
import CxDwXq from "@/views/khpx/cxdw/components/CxDwXq.vue";

export default {
  name: "CxDwCx",
  components: {CxDwXq, Jindu, KkFileView},
  dicts: ['xmgl_lxwj_pzdw', 'year', 'process_business_status'],
  watch: {
    activeName(newVal) {
      if (newVal === 'fjzl') {
        this.clickFirstNode();
      }
    },
  },
  mounted() {
    if (this.activeName === 'fjzl') {
      this.clickFirstNode();
    }
  },
  data() {
    return {
      xg: false, //判断是新增还是修改 (false新增 true修改)
      allFilesUploaded: false, // 添加这个来追踪所有文件是否上传完成
      fjUpload: false, //附件上传弹窗
      //要修改的 创新单位id
      cxdwIdEdit: "",
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      fileList: [],
      headers: {Authorization: "Bearer " + getToken()},
      number: 0,
      uploadList: [],
      fileType: ["doc", "xls", "ppt", "txt", "pdf", "docx", "zip"],
      getCxDwId: null, //从后端获取到的创新单位ID (UUID)
      activeName: 'jbzl', //初始打开的标签页
      selectedRow: null, // 保存当前选中的行
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedCxdwFj: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 创新单位表格数据
      cxdwList: [],
      // 创新单位附件表格数据
      cxdwFjList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sbNd: null,
        sbDw: null,
        idWhere: null,
        userId: null,
        deptId: null,
        processStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sbNd: [{required: true, message: "申报年度不能为空", trigger: "change"}],
        sbDw: [{required: true, message: "申报单位不能为空", trigger: "change"}],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 查询创新单位详情信息
    handleButtonClick(row) {
      this.$refs.cxDwXq.handleOpen(row);
    },
    //查询进度
    handleJinDu(row) {
      this.$refs.picture.handleOpen(row);
    },
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    /** 查询创新单位列表 */
    getList() {
      this.loading = true;
      listCxdw(this.queryParams).then(response => {
        this.cxdwList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cxdwId: this.getCxDwId,
        sbNd: new Date().getFullYear().toString(),
        sbDw: null,
        ndCz: 0,
        yfTr: 0,
        zlZs: null,
        sbGf: null,
        qyGf: null,
        rjZzq: null,
        sbLw: null,
        sbKjj: null,
        lxXm: null,
        gsKjj: null,
        zyYjJj: null,
        idWhere: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null
      };
      this.cxdwFjList = [];
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.cxdwId)
      if (ids.length === 1) {
        this.ids = ids[0]
      } else {
        this.ids = ids
      }
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('khpx/cxdw/export', {
        ...this.queryParams
      }, `cxdw_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
